package cn.xiaobai.service;

import cn.xioabai.common.utility.ExecuteUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 命令服务
 *
 * @Author yangdaji
 * @Date 2023/2/12
 * @PackageName cn.xiaobai.service
 * @ClassName CommandServer
 */
public class CommandServer {

    /**
     * 查询网络状态, 根据对应网络名称
     * 如: queryNetworkStatusByName("无线局域网适配器", "ipconfig")
     *  如果执行 ipconfig 命令后存在 "无线局域网适配器" 则返回对应的状态结果
     *
     * @param name    名称
     * @param command 执行的命令
     * @return
     */
    public String queryNetworkStatusByName(String name, String command) {
        List<String> strings = ExecuteUtil.executeToStringList(command);
        if (!CollectionUtils.isEmpty(strings)) {
            StringBuilder result = new StringBuilder();
            List<String> statusList = strings.stream()
                    .filter(str -> str.contains(name))
                    .collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(statusList)) {
                if (statusList.size() == 0) {
                    return statusList.get(0);
                } else {
                    statusList.forEach(result::append);
                }
            }
            return result.toString();
        }
        return null;
    }
}
